package com.zb.jnlxc.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSONObject;
import com.zb.jnlxc.form.MiniPageReq;
import com.zb.jnlxc.form.MiniPageRsp;
import com.zb.jnlxc.model.Admin;
import com.zb.jnlxc.model.Mould;
import com.zb.jnlxc.service.MouldService;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 12-12-29
 * Time: 下午3:30
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/MouldAction")
@SessionAttributes(value = { "user" })
public class MouldAction {
    Logger               logger = LoggerFactory.getLogger(MouldAction.class);
    //    private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
    //            .create();
    @Resource
    private MouldService mouldService;

    @ResponseBody
    @RequestMapping("/generateCode")
    public String generateCode(Integer schemeId) {
        return mouldService.generateCode(schemeId);
    }

    @ResponseBody
    @RequestMapping("/saveUpdateMould")
    public Mould saveMould(String mouldData, String operate, @ModelAttribute("user") Admin user) {
        logger.info("mouldData={}", mouldData);
        Mould mould = JSONObject.parseObject(mouldData, Mould.class);
        operate = operate == null ? "saveMould" : operate;
        if (operate.equals("saveMould")) {
            return mouldService.saveMould(mould, user);
        } else if (operate.equals("updateMould")) {
            return mouldService.updateMould(mould);
        } else {
            return null;
        }

    }

    @ResponseBody
    @RequestMapping("/saveMouldWidthFlow")
    public Mould saveMouldWidthFlow(String mouldData, String taskId,
                                    @ModelAttribute("user") Admin user) {
        logger.info("mouldData={}", mouldData);
        Mould mould = JSONObject.parseObject(mouldData, Mould.class);
        return mouldService.saveMould(mould, taskId, user);

    }

    @ResponseBody
    @RequestMapping("/getUsedMouldListByScheme")
    public MiniPageRsp getUsedMouldListByScheme(Integer schemeId) {
        return new MiniPageRsp(mouldService.getUsedMouldListByScheme(schemeId));
    }

    @ResponseBody
    @RequestMapping("/saveMouldByTaskId")
    public Mould saveMouldByTaskId(Mould mould, String taskId, @ModelAttribute("user") Admin user) {
        return mouldService.saveMould(mould, taskId, user);
    }

    @ResponseBody
    @RequestMapping("/finishMouldBySelf")
    public void finishMouldBySelf(Integer mouldDbId) {
        mouldService.finishMouldBySelf(mouldDbId);
    }

    @ResponseBody
    @RequestMapping("/loadMould")
    public MiniPageRsp loadMould(MiniPageReq page, HttpServletRequest request) {
        page.setRequest(request);
        mouldService.LoadMould(page);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    @ResponseBody
    @RequestMapping("/getMould")
    public Mould getMould(Integer mouldId) {
        return mouldService.getById(mouldId);
    }

    @ResponseBody
    @RequestMapping("/deleteMould")
    public void deleteMould(Integer mouldId) {
        mouldService.deleteMould(mouldId);
    }

    @ResponseBody
    @RequestMapping("/updateMould")
    public void updateMould(Mould mould) {
        mouldService.updateMould(mould);
    }

    @ResponseBody
    @RequestMapping("/scrapMould")
    public void scrapMould(Mould mould, @ModelAttribute("user") Admin user) {
        mouldService.scrapMould(mould, user);
    }

    @ResponseBody
    @RequestMapping("/findAllNormalMouldsByScheme")
    public List<Mould> findAllNormalMouldsByScheme(Integer schemeId) {
        return mouldService.findAllNormalMouldsByScheme(schemeId);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public void delete(Mould mould) {
        mouldService.delete(mould);
    }

    @ResponseBody
    @RequestMapping("/loadPaiChanRecord")
    public MiniPageRsp loadPaiChanRecord(MiniPageReq page, HttpServletRequest request) {
        page.setRequest(request);
        mouldService.loadPaiChanRecord(page);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    @ResponseBody
    @RequestMapping("/loadWaitingPaiChanRecord")
    public MiniPageRsp loadWaitingPaiChanRecord(MiniPageReq page, HttpServletRequest request) {
        page.setRequest(request);
        mouldService.loadWaitingPaiChanRecord(page);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    @ResponseBody
    @RequestMapping("/loadWaitingMould")
    public MiniPageRsp loadWaitingMould(Integer paichanRecordId) {
        List l = mouldService.loadWaitingMould(paichanRecordId);
        return new MiniPageRsp(l);
    }

    @ResponseBody
    @RequestMapping("/paiMo")
    public void paiMo(Integer recordId, @ModelAttribute("user") Admin user) {
        mouldService.paiMo(recordId, user);
    }

    @ResponseBody
    @RequestMapping("/selectMould")
    public void selectMould(String mouldList, Integer recordId) {
        mouldService.selectMould(mouldList, recordId);
    }

}
